package bitCalculation;

public class ShortestCompletingWord748 {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] bitmap = new int[26];
        for (int i = 0; i < licensePlate.length(); i++) {
            char c = licensePlate.charAt(i);
            if(Character.isLetter(c)){
                bitmap[Character.toLowerCase(c)-'a']++;
            }
        }
        int length = Integer.MAX_VALUE;
        String result = "";
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            int[] bmap = new int[26];
            for (int j = 0; j < word.length(); j++) {
                char c = word.charAt(j);
                bmap[Character.toLowerCase(c)-'a']++;
            }
            boolean ok = true;
            for (int j = 0; j < bitmap.length; j++) {
                if(bitmap[j]>bmap[j]){
                    ok = false;
                }
            }
            if(ok&&word.length()<length){
                length = word.length();
                result = word;
            }
        }
        return result;
    }
}
